<?php
//开启debug
$_REQUEST['debug'] = 'false';
//判断图片上传文件夹是否有写权限
if(!is_writable(PUB_DIR.'share/images/'))
	$this->ajaxReturn(404,'图片上传文件夹没有写权限 (Public/share/images)!');
//data
if(isset($_SESSION['userid']) AND $_SESSION['userid']>0){
	$data['item'] = isset($_GET['item'])?$_GET['item']:'';
	$data['subdir'] = empty($_GET['subdir'])?null:$_GET['subdir'];
	$data['table'] = isset($_GET['table'])?$_GET['table']:'';
	$data['idid'] = isset($_GET['idid'])?(int)$_GET['idid']:'';
	$params = http_build_query($data);
	$base = defined('__SHARE__')?__SHARE__.'plug/plupload/':'';
}else{
	exit('对不起,你没有操作权限!');
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Plupload上传组件</title>
<script src="<?php echo __SHARE__;?>js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="<?php echo $base;?>js/plupload.js"></script>
<!--jquery ui-->
<link rel="stylesheet" href="<?php echo $base;?>../jqueryui/jquery-ui.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $base;?>js/jquery.ui.plupload/css/jquery.ui.plupload.css" type="text/css" />
<script type="text/javascript" src="<?php echo $base;?>../jqueryui/jquery-ui.min.js"></script>
<script type="text/javascript" src="<?php echo $base;?>js/jquery.ui.plupload/jquery.ui.plupload.js"></script>
<!--i18n-->
<script type="text/javascript" src="<?php echo $base;?>js/i18n/zh-cn.js"></script>
<!--内核-->
<script type="text/javascript" src="<?php echo $base;?>js/plupload.html5.js"></script>
<script type="text/javascript" src="<?php echo $base;?>js/plupload.flash.js"></script>
<!--script type="text/javascript" src="<?php echo $base;?>js/plupload.html4.js"></script-->
<!--script type="text/javascript" src="<?php echo $base;?>js/plupload.silverlight.js"></script-->
<!--script type="text/javascript" src="<?php echo $base;?>js/plupload.gears.js"></script-->
<!--script type="text/javascript" src="<?php echo $base;?>../jqueryui/browserplus-min.js"></script-->
</head>
<body>

<script type="text/javascript">
// Convert divs to queue widgets when the DOM is ready
$(function() {
	$("#uploader").plupload({
		// General settings
		runtimes : 'html5,flash,browserplus,silverlight,gears,html4',
		url : '<?php echo Router::encodeUrl("index.php?app=photo&act=pluploadSave&".$params);?>',
		max_file_size : '<?php echo isset($_REQUEST['maxsize'])?My::decode($_REQUEST['maxsize']):'10MB';?>',//最大文件尺寸
		chunk_size : '10MB',//分块(当上传文件大于服务器接收端文件大小限制的时候，可以分多次请求发给服务器，如果不需要从设置中移出)
		unique_names : true,
		//multiple_queues : true,//是否可以多次上传,默认true
		//multi_selection: false,//false:单文件上传 true:多文件上传(默认)
		//dragdrop:false,//是否支持拖拽
		//rename: true,//重命名
		// Sort files
		sortable: true,
		// Resize images on clientside if we can
		//resize : {width : 1920, height : 1920, quality : 97},
		// Specify what files to browse for
		filters : [
			<?php
			if(isset($_REQUEST['ext']))
				echo "{title : 'files', extensions : '".My::decode($_REQUEST['ext'])."'}";
			else
				echo "{title : 'Image files', extensions : 'jpg,gif,png,jpeg'}";
			?>
		],
		// Flash settings
		flash_swf_url : '<?php echo $base;?>js/plupload.flash.swf',
		// Silverlight settings
		silverlight_xap_url : 'js/plupload.silverlight.xap',
		// PreInit events, bound before any internal events
		preinit : {
			Init: function(up, info) {
				log('[Init]', 'Info:', info, 'Features:', up.features);
			},
			UploadFile: function(up, file) {
				log('[UploadFile]', file);
				// You can override settings before the file is uploaded
				//传递表单参数 
				up.settings.multipart_params={realName:file.name}; //$_REQUEST['realName']:真实原文件名
				// up.settings.url = 'upload.php?id=' + file.id;
			}
		},
		// Post init events, bound after the internal events
		init : {
			Refresh: function(up) {
				// Called when upload shim is moved
				log('[Refresh]');
			},
			StateChanged: function(up) {
				// Called when the state of the queue is changed
				<?php if(empty($_REQUEST['debug']) OR $_REQUEST['debug']=='false'){ ?>
				if(up.state == plupload.STOPPED){
					parent.document.location.reload();
					parent.$.fn.colorbox.close();
				}
				<?php } ?>
				log('[StateChanged]', up.state == plupload.STARTED ? "STARTED" : "STOPPED");
			},
			QueueChanged: function(up) {
				// Called when the files in queue are changed by adding/removing files
				log('[QueueChanged]');
			},
			UploadProgress: function(up, file) {
				// Called while a file is being uploaded
				log('[UploadProgress]', 'File:', file, "Total:", up.total);
			},
			FilesAdded: function(up, files) {
				// Callced when files are added to queue
				log('[FilesAdded]');

				plupload.each(files, function(file) {
					log('  File:', file);
				});
			},
			FilesRemoved: function(up, files) {
				// Called when files where removed from queue
				log('[FilesRemoved]');

				plupload.each(files, function(file) {
					log('  File:', file);
				});
			},
			FileUploaded: function(up, file, info) {
				// Called when a file has finished uploading
				log('[FileUploaded] File:', file, "Info:", info);
			},
			ChunkUploaded: function(up, file, info) {
				// Called when a file chunk has finished uploading
				log('[ChunkUploaded] File:', file, "Info:", info);
			},
			Error: function(up, args) {
				// Called when a error has occured
				log('[error] ', args);
			}
		}
	});
	/*
	// Client side form validation
	$('form').submit(function(e) {
        var uploader = $('#uploader').pluploadQueue();
        // Files in queue upload them first
        if (uploader.files.length > 0) {
            // When all files are uploaded submit form
            uploader.bind('StateChanged', function() {
                if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
                    $('form')[0].submit();
                }
            }); 
            uploader.start();
        } else {
            alert('You must queue at least one file.');
        }
        return false;
    });
	*/

	//huanran.zhao
	$('#uploader_container').attr('title','');//不要title提示

});
</script>

<script>
//debug模式的log (start the debug mode URL: &debug=true)
function log() {
	<?php if(isset($_REQUEST['debug']) AND $_REQUEST['debug']=='true'){ ?>
	var str = "";
	plupload.each(arguments, function(arg) {
		var row = "";
		if (typeof(arg) != "string") {
			plupload.each(arg, function(value, key) {
				// Convert items in File objects to human readable form
				if (arg instanceof plupload.File) {
					// Convert status to human readable
					switch (value) {
						case plupload.QUEUED:
							value = 'QUEUED';
							break;
						case plupload.UPLOADING:
							value = 'UPLOADING';
							break;
						case plupload.FAILED:
							value = 'FAILED';
							break;

						case plupload.DONE:
							value = 'DONE';
							break;
					}
				}
				if (typeof(value) != "function") {
					row += (row ? ', ' : '') + key + '=' + value;
				}
			});
			str += row + " ";
		} else { 
			str += arg + " ";
		}
	});
	$('#log').append(str + "\n");
	<?php }?>
}
</script>
<?php if(isset($_REQUEST['debug']) AND $_REQUEST['debug']=='true'){ ?>
<textarea id="log" style="width:100%;height:150px;" spellcheck="false" wrap="off"></textarea>
<?php }?>

<div id="uploader">
	<p>You browser doesn't have Flash, Silverlight, Gears, BrowserPlus or HTML5 support.</p>
</div>

<style>
.plupload_scroll{max-height: 168px;}
</style>
</body>
</html>